﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSL.Package.System
{
    public class PSL_Variable
    {
        private static IDictionary<string, dynamic> myVariable;

        /// <summary>
        /// Init Variable-System
        /// </summary>
        /// <param name="Host">Host</param>
        internal static void Init(PSLRuntime Host)
        {
            // Variable
            ///Host.AddMethod("var", PSL_Variable.PSL_Var);
        }

        /// <summary>
        /// Get and set variable
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <param name="Parameter"></param>
        /// <returns></returns>
        internal static dynamic PSL_Var(string FunctionName, PSLParameter Parameter)
        {
            if (myVariable == null)
            {
                myVariable = new Dictionary<string, dynamic>();
            }

            dynamic cToReturn = "";

            if (Parameter.ContainsParameter("name", "value"))
            {
                string cName = Parameter["name"].ToString();
                dynamic cValue = Parameter["value"];

                if (myVariable.ContainsKey(cName))
                {
                    myVariable[cName] = cValue;
                }
                else
                {
                    myVariable.Add(cName, cValue);
                }

                cToReturn = cValue;
            }
            else if (Parameter.ContainsParameter("name"))
            {
                string cName = Parameter["name"].ToString();

                if (myVariable.ContainsKey(cName))
                {
                    cToReturn = myVariable[cName];
                }
            }

            return cToReturn;
        }
    }
}
